from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context

from django.shortcuts import render_to_response
import datetime

def index(request):
    now = datetime.datetime.now()
    t = get_template('index.html')
    html = t.render(Context({
    	'current_date': now,
        'template_name': 'template_name.html',
        'template_content': "template content"
    	}))
    return HttpResponse(html)


def search(request):
    error = 'not var'
    if 'q' in request.GET:
        q = request.GET['q']
        if not q:
            error = 'xx不能为空'
        else:
            books = ("li", "lia")
            users = [
            	{"name": "li", "age":24, "hobbies": ["song", "reading"]}, 
            	{"name": "lia", "age":24, "hobbies": ["song-", "reading-"]}
        	]
            cur_user = []
            for u in users:
                cur_user.append({
                    'name': u['name'],
	        	    'age': u['age'],
	        	    'is_young': u['age']<30,
	        	    'is_notlike_song': 'song' not in u['hobbies'], #u['name'].find(' ') == -1,
	        	    'is_like_reading': 'reading' in u['hobbies']
        		})
            return render_to_response('index.html', {
                'books': books,
                'users': users,
                'cur_user': cur_user,
                'query': q})            
	    #return render_to_response('index.html', locals())  #locals()包含了所有定义在当前方法的变量,也包括request变量，这依赖于你的程序, 要注意的是locals()导致了一点点开销，因为Python不得不动态创建字典
    return render_to_response('index.html', {
        'error': error})
